<template>
  <base-breadcrumb :title="$route.meta.title">
    <!-- 质量风险上报 -->
    <div
      id="base-breadcrumb"
      style="position: relative; z-index: 1"
    >
      <!-- 搜索 -->
      <searchData
        :search-data="searchArr"
        @search="search"
      />
      <!-- 表格 -->
      <a-card :bordered="false">
        <!-- <title-name title="列表"></title-name> -->
        <!--表格-->
        <list-table
          ref="table"
          :rowKey="(record, index) => record ? (record.id + index) : index"
          :columns="columns"
          :request="loadData"
          :startIndex="0"
          :isShowCountBar="false"
          :selectConfig="{ use: true, type: 'checkbox', rowKey: 'id' }"
          :export-config="{
            use: true,
            select: '质量风险上报选中',
            all: '质量风险上报全部',
            customColumns: columns.filter(item => item.dataIndex !== 'index')
          }"
          :exportConfigText="{
            select: '选中导出',
            all: '全部导出',
          }"
          :exportPermission="{select: 'qm_057_sexp_05',all: 'qm_057_aexp_06'}"
        >
          <span class="mb12" slot="prefix" slot-scope="ref">
            <a-button 
              v-btnPermission="'qm_057_add_01'" 
              type="primary" 
              @click="handleManage(0)"
            >
              新增
            </a-button>
          </span>
          <AuditSummary
            slot="bottom"
            ref="AuditSummary"
            title=" "
            :init="false"
            :countList="countList"
            :loadCount="loadCount"
            :api="api"
            apiName="count"
            :form="queryParam"
          />
          <span
            slot="sort"
            slot-scope="text, record, index"
          >{{ index + 1 }}</span>
          <more-button
            slot="actions"
            slot-scope="text, record, index"
          >
            <a
              v-btn-permission="'qm_057_loo_04'"
              @click="handleManage(2, record)"
            >查看</a>
            <a
              v-btn-permission="'qm_057_edi_02'"
              v-if="record.comStatus == '0'"
              @click="handleManage(1, record)"
            >编辑</a>
            <a
              v-btn-permission="'qm_057_del_03'"
              v-if="record.comStatus == '0'"
              class="del_btn"
              @click="handleDelete(record.id)"
            >删除</a>
          </more-button>
          <!-- 状态格式化 -->
          <StatusDisplay
            slot="statusText"
            slot-scope="text, record"
            :status-code="record.comStatus"
            :statusMap="statusMap"
          />
        </list-table>
      </a-card>
    </div>
  </base-breadcrumb>
</template>

<script>
import OrgTreeSelect from '@/components/OrgTreeSelect'
import SelectProjectType from '@/components/Select/SelectProjectType' // 工程类别
import AuditSummary from '@/views/quality/components/AuditSummary' // 汇总
import StatusDisplay from '@/components/StatusDisplay'
import MoreButton from '@/components/MoreButton' // 多按钮容器
import BasePage from '@/views/basePage'
import Selector from '@/components/Select/Selector' // 选择器
import { riskReporting } from '@/api/quality/risk/riskReporting'
const statusMap = [
  {
    value: '0',
    label: '待提交复核',
    color: 'gray'
  },
  {
    value: '1',
    label: '复核中',
    color: 'gray'
  },
  {
    value: '2',
    label: '复核通过',
    color: 'green'
  },
  {
    value: '3',
    label: '复核不通过',
    color: 'red'
  },
]
const searchArr = [
  // 搜索框
  {
      name: '所属单位',
      valueKey: 'searchQuery',
      type: OrgTreeSelect,
  },
  {
    name: '工程部位',
    type: 'input',
    value: 'projectPosition',
  },
  {
      name: '填报时间',
      value: 'fillerDate',
      type: 'dateRange',
      rangeFormat: ['fillerStartDate', 'fillerEndDate']
  },
  {
      name: '复核风险等级',
      valueKey: 'levelId',
      type: Selector,
      attrs: {
        selectType: '59'
      }
  },
  {
    name: '状态',
    value: 'status',
    type: 'select',
    data: statusMap
  },
  {
    name: '风险名称',
    type: 'input',
    value: 'name',
  },
  {
    name: '工程类别',
    type: SelectProjectType,
    valueKey: 'projectTypeId',
    showStop: true,
  },
  
]
const columns = [
  {
    title: '序号',
    dataIndex: 'index',
    width: 80,
    scopedSlots: { customRender: 'sort' }
  },
  {
    title: '分公司',
    ellipsis: true,
    dataIndex: 'branchComName'
  },
  {
    title: '项目名称',
    ellipsis: true,
    dataIndex: 'projectName'
  },
  {
    title: '工程部位',
    ellipsis: true,
    dataIndex: 'projectPosition'
  },
  {
    title: '风险名称',
    ellipsis: true,
    dataIndex: 'name'
  },
  {
    title: '风险等级',
    ellipsis: true,
    dataIndex: 'levelName',
  },
  {
    title: '填报时间',
    ellipsis: true,
    dataIndex: 'fillerDate'
  },
  {
    title: '填报人',
    ellipsis: true,
    dataIndex: 'fillerName'
  },
  {
    title: '状态',
    dataIndex: 'statusText',
    scopedSlots: { customRender: 'statusText' }
  },
  {
    title: '操作',
    dataIndex: 'actions',
    width: '150px',
    align: 'center',
    scopedSlots: { customRender: 'actions' }
  }
]

export default {
  name: 'a' + Date.now(),
  extends: new BasePage(),
  components: {
    AuditSummary,
    StatusDisplay,
    MoreButton
  },
  data() {
    this.columns = columns
    this.searchArr = searchArr
    this.statusMap = {}
    statusMap.forEach(item => {
      this.statusMap[item.value] = {
        label: item.label,
        color: item.color
      }
    })
    return {
      exportLoading: {0: false, 1: false}, // 导出加载控件
      // 表格加载
      queryParam: {}, // 查询参数
      countList: [
        {
          title: '风险总数',
          value: 'total'
        },
        {
          title: '一般风险',
          value: 'riskZero'
        },
        {
          title: '较大风险',
          value: 'riskOne'
        },
        {
          title: '重大风险',
          value: 'riskTwo'
        },
        {
          title: '特大风险',
          value: 'riskThree'
        },
        {
          title: '非质量风险',
          value: 'noQuality'
        },
      ], // 汇总列表
      api: riskReporting,
      loadCount: 0,
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        this.loadCount++
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return riskReporting.getList(requestParameters).then((res) => {
          if (res.data) {
            res.data = res.data.map(item => {
              const arr  = statusMap.filter(i => item && item.comStatus == i.value)
              if (arr.length) {
                item.statusText = arr[0].label
              }
              item.levelName = item.finalLevel ? item.finalLevel.name : ''
              return item
            })
          }
          return res
        })
      }
    }
  },
  methods: {
    /**
     * @description: 搜索
     * @param {*} value：表单对象
     */
    search(value) {
      if (value) {
        if (value.fillerDate) {
          if (value.fillerDate.length > 0) {
            value.fillerStartDate = value.fillerDate[0].format('YYYY-MM-DD')
            value.fillerEndDate = value.fillerDate[1].format('YYYY-MM-DD')
          }
          delete value.fillerDate
        }
        this.queryParam = value
      }
      this.$refs.table.refresh(true)
    },
    /**
     * @description: 跳转操作页
     * @param {*} type：操作类型： 0 填报；1调整；2 查看；3 验证；4 生成整改单
     * @param {*} id：业务id
     */
    handleManage(type, record) {
      if (record) {
        this.$router.push({
          name: 'qualityriskIdentificationriskReportingInfo',
          query: {
            id: record.id, // 业务id
            editModel: type, // 操作类型
            projectId: record.projectId,
            branchComName: record.branchComName,
            projectName: record.projectName
          }
        })
      } else {
        this.$router.push({
          name: 'qualityriskIdentificationriskReportingInfo',
        })
      }
    },
    /**
     * @description: 删除记录
     * @param {*} id：业务id
     * @return {*}
     */
    handleDelete(id) {
      let that = this
      that.$confirm({
        title: '温馨提示',
        content: '确认删除？',
        okText: '确认',
        cancelText: '关闭',
        onOk() {
          return riskReporting.handleDelete(id).then((res) => {
            that.$message.success('删除成功！')
            that.$refs.table.loadData()
          })
        }
      })
    },
  }
}
</script>